package com.cat.dataStructure02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/find-the-most-competitive-subsequence/description/
 * @create 2025/10/21 17:05
 * @since JDK17
 */

public class Solution35 {
    public int[] mostCompetitive(int[] nums, int k) {
        // 大压小
        // 最多可以弹n - k个
        int n = nums.length, top = 0, m = n - k;
        int[] stack = new int[n];
        for (int x : nums) {
            while (m > 0 && top > 0 && stack[top - 1] > x) {
                top--;
                m--;
            }
            stack[top++] = x;
        }
        int[] ans = new int[k];
        for (int i = 0; i < k; i++) {
            ans[i] = stack[i];
        }

        return ans;
    }
}
